/*
 * file: clientGUI.java
 */

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Christina Ng Gek Ling U081583X
 * @author Novita Mayasari U085510R
 */
public class clientGUI extends javax.swing.JFrame {

    /** Creates new form clientGUI */
    public clientGUI() {
        initComponents();
    }
    //create an instance of the FTP object 
    FTP ftp = null;
    String userInput = null;
    String response = null;
    String serverName = null;
    // Variables declaration
    private final String newline = "\n";

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Connect = new javax.swing.JButton();
        CurrentDirectory = new javax.swing.JButton();
        ChangeDirectory = new javax.swing.JButton();
        GetFile = new javax.swing.JButton();
        PutFile = new javax.swing.JButton();
        List = new javax.swing.JButton();
        Quit = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        Output = new javax.swing.JTextArea();
        TinyFTP = new javax.swing.JLabel();
        input = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(204, 204, 255));
        setForeground(new java.awt.Color(204, 204, 255));

        Connect.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        Connect.setText("CONNECT");
        Connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConnectActionPerformed(evt);
            }
        });

        CurrentDirectory.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        CurrentDirectory.setText("Current Directory");
        CurrentDirectory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CurrentDirectoryActionPerformed(evt);
            }
        });

        ChangeDirectory.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        ChangeDirectory.setText("Change Directory");
        ChangeDirectory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChangeDirectoryActionPerformed(evt);
            }
        });

        GetFile.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        GetFile.setText("Download File");
        GetFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GetFileActionPerformed(evt);
            }
        });

        PutFile.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        PutFile.setText("Upload File");
        PutFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PutFileActionPerformed(evt);
            }
        });

        List.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        List.setText("List File(s)");
        List.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ListActionPerformed(evt);
            }
        });

        Quit.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        Quit.setText("QUIT");
        Quit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                QuitActionPerformed(evt);
            }
        });

        Output.setColumns(20);
        Output.setEditable(false);
        Output.setRows(5);
        Output.setText("To connect: \n<ftp address> <username> <password>\n\nTo change directory:\n<directory>\n\nTo download file:\n<filename> <destination filepath>\n\nTo upload file:\n<filename> <source filepath>\n\nTo delete file:\n<filename>\n\n----------------------------------------------\n\n");
        Output.setWrapStyleWord(true);
        jScrollPane1.setViewportView(Output);

        TinyFTP.setFont(new java.awt.Font("Trebuchet MS", 1, 24)); // NOI18N
        TinyFTP.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        TinyFTP.setText("Tiny FTP Client");

        input.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        input.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inputActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton1.setText("Back to Parent Directory");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton2.setText("Delete File");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(input, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(CurrentDirectory, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ChangeDirectory, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(List, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(PutFile, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(GetFile, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)))
                    .addComponent(Quit, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                    .addComponent(Connect, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                    .addComponent(TinyFTP, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(TinyFTP, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(Connect)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(CurrentDirectory)
                            .addComponent(List))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ChangeDirectory)
                            .addComponent(PutFile))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(GetFile))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addGap(13, 13, 13)
                        .addComponent(Quit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(input, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void connect() {
        try {
            String[] i;
            userInput = input.getText();
            i = userInput.split(" ");
            if (i.length != 3) {
                Output.append("Wrong parameters" + newline);
                return;
            }
            ftp = new FTP();
            System.out.println("FTP object created.");
            response = ftp.connect(i[0], 21);
            System.out.println("FTP connected");
            Output.append(response + newline);
            response = ftp.login(i[1], i[2]);
            System.out.println("User logged in");
            Output.append(newline + response + newline);
        } catch (UnknownHostException ex) {
            Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void currDir() {
        try {
            String directory = null;
            directory = ftp.getCurrDir();
            Output.append(directory + newline);
        } catch (IOException ex) {
            Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void parentDir() {
        try {
            String directory = null;
            directory = ftp.backToParentDirectory();
            Output.append(directory + newline);
        } catch (IOException ex) {
            Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void changeDir() {
        try {
            String[] i;
            userInput = input.getText();
            i = userInput.split(" ");
            if (i.length != 1) {
                Output.append("Wrong parameters " + newline);
            } else {
                //execute changeDirectory method of the FTP object
                response = ftp.changeDir(i[0]);
                Output.append(response.toString() + newline);
            }
        } catch (IOException ex) {
            Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void listFiles() {
        try {
            String currentDirectory = null;
            currentDirectory = ftp.getCurrDir();
            System.out.println("Current directory: " + currentDirectory);
            response = ftp.listFiles(currentDirectory);
            Output.append(response + newline);
        } catch (IOException ex) {
            Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void getFile() {
        String[] i;
        userInput = input.getText();
        i = userInput.split(" ");
        if (i.length != 2) {
            Output.append("Wrong parameters" + newline);
        } else {
            String filename = i[0];
            String filepath = i[1];
            if (!filepath.endsWith("\\")) {
                filepath = filepath + "\\";
            }
            try {
                //execute downloadFile method of the FTP object
                response = ftp.getFile(filename, filepath + filename);
                Output.append(response + newline);
            } catch (IOException ex) {
                Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void storeFile() {
        String[] i;
        userInput = input.getText();
        i = userInput.split(" ");
        if (i.length != 2) {
            Output.append("Wrong parameters" + newline);
        } else {
            String filename = i[0];
            String filepath = i[1];
            if (!filepath.endsWith("\\")) {
                filepath = filepath + "\\";
            }
            try {
                response = ftp.storeFile(filename, filepath + filename);
                Output.append(response + newline);
            } catch (IOException ex) {
                Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void delete() {
        String[] i;
        userInput = input.getText();
        i = userInput.split(" ");
        if (i.length != 1) {
            Output.append("Wrong parameters" + newline);
        } else {
            try {
                response = ftp.deleteFile(i[0]);
            } catch (IOException ex) {
                Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            Output.append(response + newline);
        }
    }

    public void quit() {
        try {
            Output.append(ftp.disconnect() + newline);
        } catch (IOException ex) {
            Logger.getLogger(clientGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void ConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConnectActionPerformed
        Main.option = 1;
    }//GEN-LAST:event_ConnectActionPerformed

    private void CurrentDirectoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CurrentDirectoryActionPerformed
        Main.option = 2;
    }//GEN-LAST:event_CurrentDirectoryActionPerformed

    private void QuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QuitActionPerformed
        Main.option = 7;
    }//GEN-LAST:event_QuitActionPerformed

    private void ListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ListActionPerformed
        Main.option = 6;
    }//GEN-LAST:event_ListActionPerformed

    private void PutFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PutFileActionPerformed
        Main.option = 5;
    }//GEN-LAST:event_PutFileActionPerformed

    private void GetFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GetFileActionPerformed
        Main.option = 4;
    }//GEN-LAST:event_GetFileActionPerformed

    private void ChangeDirectoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangeDirectoryActionPerformed
        Main.option = 3;
    }//GEN-LAST:event_ChangeDirectoryActionPerformed

    private void inputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_inputActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Main.option = 8;
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Main.option = 9;
    }//GEN-LAST:event_jButton2ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ChangeDirectory;
    private javax.swing.JButton Connect;
    private javax.swing.JButton CurrentDirectory;
    private javax.swing.JButton GetFile;
    private javax.swing.JButton List;
    private javax.swing.JTextArea Output;
    private javax.swing.JButton PutFile;
    private javax.swing.JButton Quit;
    private javax.swing.JLabel TinyFTP;
    private javax.swing.JTextField input;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
